package com.jsjosem.aftersales.ui.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.adapter.HomeRankListAdapter;
import com.jsjosem.aftersales.api.RetrofitClient;
import com.jsjosem.aftersales.api.WorkOrderApi;
import com.jsjosem.aftersales.cache.UserCache;
import com.jsjosem.aftersales.data.model.HomeRank;
import com.jsjosem.aftersales.data.response.BaseResponse;
import com.jsjosem.aftersales.data.response.WorkRankingsResponse;
import com.jsjosem.aftersales.databinding.FragmentHomeBinding;
import com.jsjosem.aftersales.request.AsyncRequest;
import com.jsjosem.aftersales.ui.MainActivity;
import com.jsjosem.aftersales.ui.event.EventActivity;
import com.jsjosem.aftersales.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollUtil;
import retrofit2.Call;
import retrofit2.Retrofit;


public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;
    private ConstraintLayout view, workOrderItem, noticeItem;
    private Context context;
    private ListView rankList;
    private List<WorkRankingsResponse> homeRankList;
    private HomeRankListAdapter homeRankListAdapter;
    private TextView incompleteWoCountTv, ToBeAcceptedWoCountTv, inProgressWoCountTv, inAbnormalWoCountTv;
    private TextView tvUserName;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(inflater, container, false);
        view = binding.getRoot();
        context = view.getContext();

        initView();
        initData();
        String name = UserCache.getInstance().getUser().getName();
        tvUserName.setText("你好，" + name);
        workOrderItem.setOnClickListener(view -> {
            MainActivity mainActivity = (MainActivity) getActivity();
            mainActivity.binding.navView.setSelectedItemId(R.id.navigation_work_tickets);
        });
        noticeItem.setOnClickListener(view -> {
//            MainActivity mainActivity = (MainActivity) getActivity();
//            mainActivity.binding.navView.setSelectedItemId(R.id.navigation_notifications);
            Intent intent = new Intent(getContext(), EventActivity.class);
            startActivity(intent);
        });
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private void initView() {
        incompleteWoCountTv = view.findViewById(R.id.home_incomplete_num);
        ToBeAcceptedWoCountTv = view.findViewById(R.id.home_to_be_accepted_num);
        inProgressWoCountTv = view.findViewById(R.id.home_have_hand_num);
        inAbnormalWoCountTv = view.findViewById(R.id.home_abnormal_work_order_num);
        workOrderItem = view.findViewById(R.id.home_item_work_order);
        tvUserName = view.findViewById(R.id.home_user_name);
        rankList = view.findViewById(R.id.home_rank_lv);
        noticeItem = view.findViewById(R.id.home_item_spare_part);
    }

    private void initData() {
        homeRankList = new ArrayList<>();
        homeRankListAdapter = new HomeRankListAdapter(context, R.layout.home_rank_item_list, homeRankList);
        rankList.setAdapter(homeRankListAdapter);

        Retrofit mRetrofit = RetrofitClient.getInstance();
        WorkOrderApi workOrderApi = mRetrofit.create(WorkOrderApi.class);
        Call<BaseResponse<List<WorkRankingsResponse>>> rankingCall = workOrderApi.woRankings();
        Call<BaseResponse<Map<String, Integer>>> getStatusCounts = workOrderApi.getStatusCounts();
        AsyncRequest<BaseResponse<Map<String, Integer>>> request = new AsyncRequest<>();
        AsyncRequest<BaseResponse<List<WorkRankingsResponse>>> rankingReq = new AsyncRequest<>();
        rankingReq.sendRequest(rankingCall, res -> {
            List<WorkRankingsResponse> data = res.getData();
            for (int i = 0; i < data.size(); i++) {
                homeRankList.add(data.get(i));
                homeRankList.get(i).setRank(i + 1);
            }
            homeRankListAdapter.notifyDataSetChanged();
            ViewUtils.setListViewHeightBasedOnChildren(rankList);
        }, err -> {
            Toast.makeText(context, err.toString(), Toast.LENGTH_SHORT).show();
        });
        request.sendRequest(getStatusCounts, res -> {
            if (res != null) {
                Map<String, Integer> data = res.getData();
                incompleteWoCountTv.setText(data.get("未完成").toString());
                ToBeAcceptedWoCountTv.setText(data.get("已创建").toString());
                inProgressWoCountTv.setText(data.get("已指派").toString());
                inAbnormalWoCountTv.setText(data.get("已完成").toString());
            }
        }, err -> {
            Toast.makeText(context, err.toString(), Toast.LENGTH_SHORT).show();
        });
    }
}